<?php
namespace Question\Controller;
use Common\Controller\AdminBase;
class QuestionController extends AdminBase {
    private $Question = NULL;
    private $Doubt = NULL;
    protected function _initialize() {
        parent::_initialize();
        $this->Question = D('Question/Question');
        $this->Doubt = D('Question/Doubt');
    }
    //后台管理首页
    public function index() {
        if (IS_POST) {
            //排序
            $id = I('post.id');
            $listorder = I('post.listorder');
            if (empty($id)) {
                $this->error('请选择需要排序的信息！');
            }
            foreach ($id as $k => $id) {
                $this->Question->where(array('id' => $id))->save(array('listorder' => $listorder[$k]));
            }
            $this->success('排序更新成功！', U('Question/index'));
            exit;
        }
        //条件
        $where = array();
        $keyword = I('get.keyword');
        if ($keyword) {
            $where['zhiwei'] = array('LIKE', "%{$keyword}%");
            $this->assign('keyword', $keyword);
        }
        $count = $this->Question->where($where)->count();
        $page = $this->page($count, 20);
        $data = $this->Question->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array( "id" => "DESC"))->select();
        foreach ($data as &$val){
            switch ($val['kemu']){
                case '1':
                    $val['kemu']='物理';
                    break;
                case '2':
                    $val['kemu']='化学';
                    break;
                case '3':
                    $val['kemu']='语文';
                    break;
                case '4':
                    $val['kemu']='数学';
                    break;
                case '5':
                    $val['kemu']='生物';
                    break;
                case '6':
                    $val['kemu']='地理';
                    break;
                case '7':
                    $val['kemu']='政治';
                    break;
                case '8':
                    $val['kemu']='历史';
                    break;
            }
            if($val['updatetime']=='0'){
                $val['updatetime']="";
            }else{
                $val['updatetime']=date('Y-m-d H:i:s');
            }
        }
        //统计
        $done   = $this->Question->where(array('is_back' => 1))->count();
        $undone = $this->Question->where(array('is_back' => 0))->count();
        $this->assign('data', $data);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->assign('done', $done);
        $this->assign('undone', $undone);
        $this->display();
    }
    public function answer() {
        if (IS_POST) {
            $post = $_POST;
            if ($this->Question->answer($post)) {
                $this->success('答疑成功！', U('Question/index'));
            } else {
                $error = $this->Question->getError();
                $this->error($error ? $error : '答疑失败！');
            }
        } else {
            $Id = I('get.id', 0, 'intval');
            if (empty($Id)) {
                $this->error('请选择需要回复的信息！');
            }
            $info = $this->Question->getInfo($Id);
            switch ($info['kemu']){
                case '1':
                   $info['kemu']="物理";
                    break;
                case '2':
                    $info['kemu']="化学";
                    break;
                case '3':
                    $info['kemu']="语文";
                    break;
                case '4':
                    $info['kemu']="数学";
                    break;
                case '5':
                    $info['kemu']="生物";
                    break;
                case '6':
                    $info['kemu']="地理";
                    break;
                case '7':
                    $info['kemu']="政治";
                    break;
                case '8':
                    $info['kemu']="历史";
                    break;
                default :
                    $info['kemu']="未知科目";
            }
            if (empty($info)) {
                $this->error('该信息不存在！');
            }
            $this->assign('info', $info);
            $this->display();
        }
    }
    public function delete() {
        if (IS_POST) {
            $Id = I('post.id');
        } else {
            $Id = I('get.id', 0, 'intval');
        }
        if (empty($Id)) {
            $this->error('请选择需要删除的留言信息！');
        }
        if ($this->Question->deleteQuestion($Id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
}
